//询价应收列表

interface IInquiryReceiveManagentViewModel extends IBaseViewModel<IInquiryReceiveQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<InquiryListItemResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 调度专员下拉
     */
    dispatchOfficerDropDown: EmployeeListItemResponse[];
    /**
     * 应收报价事件
     */
    inquiryReceiveEdit: (id: string) => void;
    /**
     * 查询
     */
    query:()=>void;
    /**
     * 列表高
     */
    heightA:number;
    /**
     * 询价状态
     */
    states:string;
}

/**
 * 查询条件
 */
interface IInquiryReceiveQueryTerms {
    /**
     * 总询价编号
     */
    inquiryId: string;
    /**
     * 子询价编号
     */
    inquiryChildId: string;
    /**
     * 客户单位
     */
    clientName: string;
    /**
     *  调度专员
     */
    dispatchOfficer: string;
    /**
     * 开始时间
     */
    beginTime: string;
    /**
     * 结束时间
     */
    endTime: string;
    /**
     * 发货地址
     */
    startAddress: string;
    /**
     * 送货地址
     */
    endAddress: string;
}

class InquiryReceiveController extends ListController<IInquiryReceiveManagentViewModel, IInquiryReceiveQueryTerms>{
    gridApi: uiGrid.IGridApiOf<InquiryListItemResponse>;
    orderBy: number = 0;
    asc: boolean = true;

    constructor($scope: IInquiryReceiveManagentViewModel, private inquiryService: IInquiryService, private valueService: IValueService, private $ngBootbox: BootboxService,
        private employeeService: IEmployeeService, private $state: angular.ui.IStateService,private $location:ng.ILocationService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();

        this.$scope.$on("WildDog:Inquiry2", this.onWilddogOrderEvent);
        this.$scope.queryTerms = {
            inquiryId: "",
            inquiryChildId: "",
            clientName: "",
            dispatchOfficer: "",
            beginTime: "",
            endTime: "",
            startAddress: "",
            endAddress: ""
        };

        this.$scope.states = "4,7";

        this.$scope.inquiryReceiveEdit = this.inquiryReceiveEdit;

        this.$scope.gridOptions = {
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: '总询价编号', field: 'inquiryId', enableColumnMenu: false, width: '9%' },
                { displayName: "子询价编号", field: 'inquiryChildId', enableColumnMenu: false, width: '10%' },
                { displayName: "客户单位", field: 'clientName', enableColumnMenu: false, width: '10%' },
                { displayName: '发货地址', field: 'startAddress', enableColumnMenu: false, enableSorting: false, width: '10%' },
                { displayName: '送货地址', field: 'endAddress', enableColumnMenu: false, enableSorting: false, width: '10%' },
                { displayName: '货物名称', field: 'goodsName', enableColumnMenu: false },
                { displayName: '货物数量', field: 'goodsQuantity', enableColumnMenu: false },
                { displayName: '所需车长', field: 'carLength', enableColumnMenu: false, },
                { displayName: '调度专员', field: 'dispatchOfficer', enableColumnMenu: false },
                { displayName: '下单时间', field: 'createTime', enableColumnMenu: false, width: '9%' },
                { displayName: '紧急程度', field: 'urgency', enableColumnMenu: false },
                { displayName: '询价状态', field: 'state', enableColumnMenu: false },
                {
                    displayName: "操作",
                    field: '操作',
                    cellTemplate: '<a ng-click="grid.appScope.inquiryReceiveEdit(row.entity.id,row.entity.state)"  class="fa fa-sign-in m-l-xs   primary text-info" title="应收报价 "></a>',
                    enableColumnMenu: false,
                    enableSorting: false,
                    width: '6%'
                }],
            onRegisterApi: (gridApi) => {
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                gridApi.core.on.sortChanged(this.$scope, this.sortChange);
            }
        };

        $('#opendataTime').click(function () {
            $('#dataTime').datetimepicker('show');
        });
        $('#opendataTimeEnd').click(function () {
            $('#dataTimeEnd').datetimepicker('show');
        });

        this.query();
        this.$scope.query = this.query;
        this.loadDispatchOfficerDropDown();
    }

    localHistory():void{
        super.localHistory(this.$state);
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }

    /**
     * 加载数据
     */
    loadData(queryTerms: IInquiryAuditQueryTerms, skip: number, count: number): void {
        this.inquiryService.getList(this.orderBy.toString(), this.asc, queryTerms.inquiryId, queryTerms.inquiryChildId, queryTerms.clientName, queryTerms.dispatchOfficer, "",
            queryTerms.startAddress, queryTerms.endAddress, this.$scope.states, queryTerms.beginTime, queryTerms.endTime, skip, count).then(result => {
                this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;

                this.$scope.heightA = (this.$scope.gridOptions.data.length *39)+95;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }

    /**
     * 监听野狗事件
     */
    onWilddogOrderEvent = (event: ng.IAngularEvent, data: any) => {
        this.query();
    }

    /**
     * 排序事件触发
     */
    sortChange=(grid: uiGrid.IGridInstanceOf<InquiryListItemResponse>, sortColumns: Array<uiGrid.IGridColumnOf<InquiryListItemResponse>>)=> {
        if (sortColumns.length == 0) {
            this.reload();
        } else {
            let sortItem = sortColumns[0];
            switch (sortItem.name) {
                case "inquiryId": {
                    this.orderBy = 1;
                } break;
                case "inquiryChildId": {
                    this.orderBy = 2;
                } break;
                case "clientName": {
                    this.orderBy = 3;
                } break;
                case "goodsName": {
                    this.orderBy = 4;
                } break;
                case "goodsQuantity": {
                    this.orderBy = 5;
                } break;
                case "carLength": {
                    this.orderBy = 6;
                } break;
                case "createTime": {
                    this.orderBy = 7;
                } break;
                case "urgency": {
                    this.orderBy = 8;
                } break;
                case "state": {
                    this.orderBy = 9;
                } break;
            }
            this.asc = sortItem.sort.direction == "asc";
            this.reload();
        }
    }

    /**
     * 调度专员下拉
     */
    loadDispatchOfficerDropDown(): void {
        this.employeeService.getbytype(CommissionerType.Dispatch.toString(), 0, -1).then(result => {
            this.$scope.dispatchOfficerDropDown = result.data.data;
        });
    }

    /**
     * 应收报价
     */
    inquiryReceiveEdit(id,state?:string): void {
        this.$state.go('app.inquiry.Inquiry_pay', { id: id, name: 'receive',state:state })
    }
}

angular.module("tms").controller("inquiryReceivaController", ["$scope", "inquiryService", "valueService", "$ngBootbox", "employeeService", "$state","$location", InquiryReceiveController]);